/**
 * @(#) HouseFundProxy.as
 *
 * @date		2009. 04. 23.
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				특별공제 보험료 입력 관리 
 */
package com.skt.common_hr.yearadjustment.model
{
	
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.puremvc.DataReceiver;
	import com.skt.common_hr.yearadjustment.model.vo.HouseFundVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class HouseFundProxy extends CommonProxy
	{
		public static const NAME:String = "HouseFundProxy";
		
		public static const LOAD_HOUSE_FUND_COMPLETE:String = "loadHouseFundComplete";
		public static const LOAD_HOUSE_FUND_FAILED:String = "loadHouseFundFailed";
		public static const SAVE_HOUSE_FUND_COMPLETE:String = "saveHouseFundComplete";
		public static const SAVE_HOUSE_FUND_FAILED:String = "saveHouseFundFailed";
		
		public static const LOAD_COMBO_TYPE_HOUSEFUND_COMPLETE:String = "LOAD_COMBO_TYPE_HOUSEFUND_COMPLETE";
		
		
		public var msgStr:String;
		public var houseFundList:ArrayCollection = new ArrayCollection();
		

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function HouseFundProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//CursorManager.setBusyCursor();
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			param["I_YEAGBN"] = "E5";		//01:보험비, 02:교육비, 03:시뇽카드 비용, 04: 현금 비용, 05:지로 비용, 06:기부금, 07:의료비
			param["I_PROC_TYPE"] = "D";		//D:조회, S:저장
			loadData("Z_HR_0085_NEW", param, result, fault);
		}
		
		public function save(parnr:String, year:String, donationArr:Array):void{
			//CursorManager.setBusyCursor();
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			/* for(var i:int  = 0 ; i < donationArr.length; i++){
				trace("YEAGBN : " + donationArr[i].YEAGBN);
				trace("NATCT : " + donationArr[i].NATCT);
				trace("NATAM : " + donationArr[i].NATAM);
				trace("REGNO : " + donationArr[i].REGNO);
				trace("COMCD : " + donationArr[i].COMCD);
			} */
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = parnr;
			param["I_YEAGBN"] = "E5";		//01:보험비, 02:교육비, 03:시뇽카드 비용, 04: 현금 비용, 05:지로 비용, 06:기부금, 07:의료비
			param["I_PROC_TYPE"] = "S";		//D:조회, S:저장
			var table:Object = new Object();
			table["ITAB3"] = donationArr;
			saveData("Z_HR_0085_NEW", param, table, saveResult, saveFault);
		}
		
		//		2014
		public function typeload():void
		{
			var param:Object = new Object();
			param["I_YEAGBN"] = "E6";
			loadMultiData("Z_HR_0096_NEW", param, typeResult); 
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				houseFundList = parseHouseFund(event.result.ITAB3);
				facade.sendNotification(LOAD_HOUSE_FUND_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(LOAD_HOUSE_FUND_FAILED, this);
			}
		}

		public var comboInType:ArrayCollection = new ArrayCollection();
		public function typeResult(event:DataReceiver):void
		{
			if(retCode==0) {  
				comboInType = event.result.ITAB;
				facade.sendNotification(LOAD_COMBO_TYPE_HOUSEFUND_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				Alert.show(msgStr);
			}
		}		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(LOAD_HOUSE_FUND_FAILED, this);
		}
		
		//저장 완료
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(SAVE_HOUSE_FUND_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(SAVE_HOUSE_FUND_FAILED, this);
			}
		}
		
		//저장실패
		private function saveFault(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(SAVE_HOUSE_FUND_FAILED, this);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 특별공제 보험료 Value Object
		 */
		private function parseHouseFund(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in arr){
				var vo:HouseFundVO = new HouseFundVO();
				vo.BEGDA	= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA	= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.BETRG	= rec["BETRG"];
				
				vo.LDNAM	= rec["LDNAM"];
				vo.LDREG		= rec["LDREG"];
				vo.ADDRE		= rec["ADDRE"];
				
				vo.HOUTY		= rec["HOUTY"];
				vo.HOUTX	= rec["HOUTX"];
				vo.FLRAR		= rec["FLRAR"];
				
				returnArr.addItem(vo);
			}
			
			return returnArr;
		}
		
	}
}